﻿using System;
using VsxFactory.Modeling.VisualStudio;
using System.Collections.Generic;

namespace VsxFactory.Modeling.Strategies.VisualStudio
{
    class VSSolutionExplorer : IVsSolutionExplorer
    {
        private SolutionManagerService solutionManager;

        public VSSolutionExplorer(IServiceProvider serviceProvider)
        {
            System.Diagnostics.Contracts.Contract.Requires(serviceProvider != null);
            solutionManager = new SolutionManagerService(serviceProvider);
        }

        public IVsSolution CurrentSolution
        {
            get { return solutionManager.CurrentSolution != null ? new VSSolution(this, solutionManager.CurrentSolution) : null; }
        }

        public IVsProjectItem SelectedItem
        {
            get
            {
                var dte = solutionManager.ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
                if (dte.SelectedItems == null || dte.SelectedItems.Count == 0)
                    return null;
                var item = dte.SelectedItems.Item(1);
                return item == null || item.ProjectItem == null || item.ProjectItem.FileCount == 0 ? null : CurrentSolution.FindByPath<IVsProjectItem>(item.ProjectItem.FileNames[1]);
            }
        }

        public void DisableReferenceEventsOnProject(string projectName)
        {
            // TODO
        }

        public void EnableReferenceEventsOnProject()
        {
            // TODO
        }


        public string GetExportedProjectTemplatesDir()
        {
            return solutionManager.GetExportedProjectTemplatesDir();
        }
    }
}
